<?php

namespace App\Enum;


abstract class BaseEnum
{
    /**
     * @return array
     */
    abstract public static function getMap(): array;

    /**
     * @param $key
     * @return string
     */
    public static function getValue($key): string
    {
        return static::getMap()[$key] ?? '';
    }

    /**
     * 返回组装好的键值对给前端
     * $str true=键值是字符串，false=键值是数字
     * @return array
     */
    public static function getList($str=true,$name='name',$id='id')
    {
        $data = static::getMap();
        $array = [];
        foreach ($data as $key => $value) {
            $info[$name] = $value;
            $info[$id] = $key;
            $array[] = $info;
        }
        if($str){
            return opt_arr($array);
        }else{
            return $array;
        }
    }

    /**
     * @param array $keys
     * @return array
     */
    public static function getValues(array $keys) : array
    {
        return array_filter($keys,static::getMap());
    }

    /**
     * @return array
     */
    public static function getKeys(): array
    {
        return array_keys(static::getMap());
    }

    public static function getSearchConfig()
    {
        return array_map(function ($key, $value){
            return array('id' => $key, 'value' => $value);
        },array_keys(static::getMap()),static::getMap());
    }
}